﻿using Koala.Pro.CodeGenerator.UI.Common;
using Koala.Pro.CodeGenerator.UI.DatabaseModel;
using System.Reflection;

namespace Koala.Pro.CodeGenerator.UI
{
    public partial class FrmRegisterDb : Form
    {
        private string databaseType = string.Empty;

        public string DatabaseType
        {
            get
            {
                return databaseType;
            }
            set
            {
                databaseType = value;
                txtDatabaseType.Text = databaseType;
                txtDatabaseType.ReadOnly = true;
                InitData();
            }
        }

        public FrmRegisterDb()
        {
            InitializeComponent();
        }

        private void InitData()
        {
            cmbDbType.Enabled = false;
            if (string.IsNullOrEmpty(DatabaseType))
            {
                return;
            }
            DatabaseSetting[] array = DatabaseConfig.ReadSettings();
            DatabaseSetting[] array2 = array;
            foreach (DatabaseSetting databaseSetting in array2)
            {
                if (databaseSetting.DatabaseType.Equals(DatabaseType, StringComparison.OrdinalIgnoreCase))
                {
                    ConnectionInfo connectionInfo = new ConnectionInfo(databaseSetting.ConnectString);
                    txtServer.Text = connectionInfo.Server;
                    txtUser.Text = connectionInfo.UserId;
                    txtPassword.Text = connectionInfo.Password;
                    break;
                }
            }
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            if (txtDatabaseType.Text.Length == 0)
            {
                MessageUtil.ShowWarning("数据库配置项名称不能为空。");
                return;
            }
            ConnectionInfo connectionInfo = new ConnectionInfo();
            connectionInfo.Server = txtServer.Text;
            connectionInfo.Password = txtPassword.Text;
            connectionInfo.UserId = txtUser.Text;
            DatabaseSetting databaseSetting = new DatabaseSetting();
            databaseSetting.ConnectString = connectionInfo.ConnectionString;
            databaseSetting.DatabaseType = txtDatabaseType.Text;
            databaseSetting.AssemblyType = cmbDbType.Text;
            string text = cmbDbType.Text;  
            databaseSetting.AssemblyName = Assembly.GetExecutingAssembly().GetName().Name;
            try
            {
                if (!string.IsNullOrEmpty(DatabaseType))
                {
                    DatabaseConfig.Modify(databaseSetting);
                }
                else
                {
                    DatabaseConfig.Add(databaseSetting);
                }
                MessageUtil.ShowTips("保存成功");
                Close();
            }
            catch (Exception ex)
            {
                MessageUtil.ShowError(string.Format("无法保存{0}的配置信息", databaseSetting.DatabaseType) + ex.Message);
            }
        }
    }
}
